% 实线性Klein-Gordon方程的2D显式求解。
% Gitee Repo

clc
clear

L=2;
dx = 0.01;
dt = 0.001;

m=1;
hbar = 1;
c=1;

sigma = (c*dt/dx)^2;
dtmch = dt^2*m^2*c^4/hbar^2;

[x y] = meshgrid(-L:dx:L);
n = size(x, 1);

u0 = e.^(-10*(x.^2+y.^2));
u1 = u0;
u2 = u0;

figure();
imgind=0;
TICK=10000;

for tick = 3:TICK
    diff_i = u1(1:n-2,2:n-1)-2*u1(2:n-1,2:n-1) + u1(3:n,2:n-1);
    diff_j = u1(2:n-1, 1:n-2)-2*u1(2:n-1, 2:n-1) + u1(2:n-1, 3:n);
    u2(2:n-1,2:n-1) =2*u1(2:n-1,2:n-1) - u0(2:n-1,2:n-1) + sigma*diff_i + sigma*diff_j - dtmch*u1(2:n-1,2:n-1);

    u2(1,:) = zeros(1,n); %固定边界
    u2(n,:) = zeros(1,n);
    u2(:,1) = zeros(n,1);
    u2(:,n) = zeros(n,1);

	u0 = u1;
	u1 = u2;

    if mod(imgind,100) == 0
		clf
		hold on
		axis equal
        axis([-L L -L L -1 1])
        xlabel('x')
        ylabel('y')
        caxis([-1 1])

        view(30,45);
        h = surf(x, y, u2);
        set(h,'EdgeColor','none');
		drawnow
		pause(0.01)
    end
    imgind++;
end


